<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Token List</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">


    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>
    <link type="text/css" rel="stylesheet" />
    <style>
        .loading-box {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.7);
            z-index: 999;
        }

        .spinner-box {
            width: 300px;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            -webkit-transform: translate(-50%, -50%);
        }

        .ellipsis-default {
            max-width: 190px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
        }

        .ellipsis-default:hover {
            word-break: break-all;
            text-overflow: unset;
            white-space: pre-line;
        }
    </style>
</head>

<body>
    <div class="container hidden">
        <h1 class="text-center">Token List</h1>
        <div class="">
            <table class="table table-hover table-bordered table-striped">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td>System Name</td>
                        <td id="simpleToken">Token</td>
                        <td id="privateKey" class="hidden">Private Key</td>
                        <td id="publicKey" class="hidden">Public Key</td>
                        <td id="option">Option</td>
                    </tr>
                </thead>
                <tbody id="data">
                </tbody>
                <tfoot>

                </tfoot>
            </table>
            <p id="msg" class="text-center"></p>
            <button type="button" class="btn btn-danger" id="delAll">全部删除</button>
            <a href="javascript:;" class="btn btn-link" id="tokenRegister">注册token</a>
        </div>
    </div>

    <div class=" loading-box">
                <div class="spinner-box">
                    <img src="/img/loading.gif">
                </div>
        </div>
</body>
<script type="text/javascript">
	var basePath = "/itsu-token";
    $(function () {

        setTimeout(function () {
            init();
        }, 1000)

        function init() {
            $.ajax({
                type: "get",
                url: basePath + "/tokenData/list",
                data: {},
                success: function (res) {
                    if (res.data && res.type) {
                        if (res.type == 'simple') {
                            $("#simpleToken").removeClass("hidden");
                            $("#privateKey").addClass("hidden");
                            $("#publicKey").addClass("hidden");
                            if (res.data.length == 0) {
                                $("#msg").append("no data found");
                            }
                            console.log(res)

                            $.each(res.data, function(index, item){
                                var content = "<tr>";
                                content += "<td>" + item.id + "</td>\n";
                                content += "<td>" + item.system_name + "</td>\n";
                                content += "<td>" + item.token + "</td>\n";
                                content += `<td>
                                    <button type="button" class="btn btn-warning btn-xs single-del">删除</button>
                                </td>`;
                                content += "</tr>"
                                $("#data").append(content);
                            })

                            
                        } else if (res.type == 'rsa') {
                            $("#simpleToken").addClass("hidden");
                            $("#privateKey").removeClass("hidden");
                            $("#publicKey").removeClass("hidden");
                            if (res.data.length == 0) {
                                $("#msg").append("no data found");
                            }

                            // res.data.forEach(ele => {
                            //     var content = "<tr>";
                            //     for (const e in ele) {
                            //         if (e == 'private_key' || e == 'public_key') {
                            //             content += "<td class='ellipsis-default'>" + ele[
                            //                     e] +
                            //                 "</td>\n";
                            //         } else {
                            //             content += "<td>" + ele[e] + "</td>\n";
                            //         }
                            //     }
                            //     content += `<td>
                            //                     <button type="button" class="btn btn-warning btn-xs single-del">删除</button>
                            //                 </td>`;
                            //     content += "</tr>"
                            //     $("#data").append(content);
                            // });

                            $.each(res.data, function(index, item){
                                var content = "<tr>";
                                content += "<td>" + item.id + "</td>\n";
                                content += "<td>" + item.system_name + "</td>\n";
                                content += "<td class='ellipsis-default'>" + item.private_key + "</td>\n";
                                content += "<td class='ellipsis-default'>" + item.public_key + "</td>\n";
                                content += `<td>
                                    <button type="button" class="btn btn-warning btn-xs single-del">删除</button>
                                </td>`;
                                content += "</tr>"
                                $("#data").append(content);
                            })

                        } else {
                            $.confirm({
                                title: 'Error!',
                                content: 'Unknow Type',
                                type: 'orange',
                                typeAnimated: true,
                                buttons: {
                                    close: function () {}
                                }
                            });
                            return false;
                        }
                        $(".container").removeClass("hidden")
                    } else {
                        $.confirm({
                            title: 'Error!',
                            content: 'Token data interface error',
                            type: 'orange',
                            typeAnimated: true,
                            buttons: {
                                close: function () {}
                            }
                        });
                    }
                },
                error: function (error) {
                    $.alert(error.responseText)
                },
                complete: function () {
                    hideLoading();

                    $(".single-del").on('click', function () {
                        var id = $(this).parent().parent().children().eq(0).text();
                        $.confirm({
                            title: '提示!',
                            content: '你确认要删除id=' + id + '的数据吗!',
                            buttons: {
                                confirm: {
                                    text: 'confirm',
                                    btnClass: 'btn-blue',
                                    action: function () {
                                        $.ajax({
                                            type: "delete",
                                            url: basePath + "/tokenData/" + id,
                                            data: {},
                                            success: function (res) {
                                                $.dialog(res);
                                                setTimeout(
                                                    function () {
                                                        window
                                                            .location
                                                            .reload();
                                                    }, 500);
                                            },
                                            error: function (error) {
                                                $.dialog(error
                                                    .responseText
                                                    );
                                            }
                                        });
                                    }
                                },
                                cancel: function () {
                                    // $.alert('Canceled!');
                                }
                            }
                        });
                    })

                }
            });
        }


        function showLoading() {
            $(".loading-box").css("display", "")
        }

        function hideLoading() {
            $(".loading-box").css("display", "none");
        }


        $("#tokenRegister").on('click', function () {
            $.ajax({
                type: "get",
                url: basePath + "/tokenRegisterUrl",
                data: {},
                success: function (res) {
                	if(res.indexOf("/") != 0){
						res = basePath + "/" + res;                        		
                	} else {
                		res = basePath + res;
                	}
                    top.location.href = res;
                }
            });

        })

        $("#delAll").on('click', function () {
            $.confirm({
                title: '提示!',
                content: '你确认要删除全部数据吗？',
                buttons: {
                    confirm: {
                        text: 'confirm',
                        btnClass: 'btn-blue',
                        action: function () {
                            //TODO DELETE ALL
                            $.ajax({
                                type: "delete",
                                url: basePath + "/tokenData/all",
                                data: {},
                                success: function (res) {
                                    $.dialog(res);
                                    setTimeout(function () {
                                        window.location.reload();
                                    }, 500);
                                },
                                error: function (error) {
                                    $.dialog(error.responseText);
                                }
                            });
                        }
                    },
                    cancel: function () {
                        // $.alert('Canceled!');
                    }
                }
            });
        })

    })
</script>

</html>